﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using HPIT.RentHouse.Common;
using HPIT.RentHouse.IService;

namespace HPIT.RentHouse.Web.Controllers
{
    public class UserController : Controller
    {
        private readonly IUserService _userService;
        public UserController(IUserService userService)
        {
            _userService = userService;
        }
        // GET: User
        public ActionResult Register()
        {
            return View();
        }
        [HttpPost]
        public ActionResult SendSMS(string mobile)
        {
            string keyId = ConfigurationManager.AppSettings["AccessKeyID"];
            string keySecret = ConfigurationManager.AppSettings["AccessKeySecret"];
            long code = SMSHelper.getRandom();
            AlibabaCloud.SDK.Dysmsapi20170525.Client client = AliyunSendSms.CreateClient(keyId, keySecret);
            AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsRequest sendSmsRequest = new AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsRequest
            {
                PhoneNumbers = mobile,
                SignName = "April",
                TemplateCode = "SMS_275850001",
                TemplateParam = "{\"code\":\""+ code + "\"}",
            };
            AlibabaCloud.TeaUtil.Models.RuntimeOptions runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
            AlibabaCloud.SDK.Dysmsapi20170525.Models.SendSmsResponse resp = client.SendSmsWithOptions(sendSmsRequest, runtime);
            AlibabaCloud.TeaConsole.Client.Log(AlibabaCloud.TeaUtil.Common.ToJSONString(resp));
            //var result = _userService.SendSMS(mobile);
            return Json(new AjaxResult(ResultState.Success,"发送成功"));
        }

        [HttpPost]
        public ActionResult SendEmail(string email)
        {
            var result = _userService.SendEmail(email);
            return Json(result);
        }
    }
}